﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bird.OAWeb.Domain.DTO;
using Bird.OAWeb.Domain.Table;
using Bird.OAWeb.EF;
using Bird.OAWeb.Ropesitory;
using Bird.OAWeb.Service.IService;

namespace Bird.OAWeb.Service.Service
{
    public class UserService : IUserService
    {
        IBaseRopesitory<User> userrepository;
        public UserService(IBaseRopesitory<User> userrepository)
        {
            this.userrepository = userrepository;
        }

        public UserDTO Loggin(UserDTO use)
        {
            var f = userrepository.GetList().Where(x => x.UserName.Equals(use.UserName) && x.Password.Equals(use.Password)).SingleOrDefault();
            if (f != null)
            {
                return use;
            }

            return null;
        }

        public int Logginadd(User obj)
        {
            return userrepository.Inset(obj);
        }

        public User UserBind(int id)
        {
            return userrepository.GetById(id);
        }

        public bool UserDelete(int id)
        {
            return userrepository.DeleteById(id);
        }

        public PageList<User> UserShow(int pageIndex = 1, int pageSize = 2)
        {
            var list = from s in userrepository.GetList()
                       select new User
                       {
                           UserName = s.UserName,
                           UserId= s.UserId,
                           UpdateTime= s.UpdateTime,
                           CreatedTime= s.CreatedTime,
                           State= s.State,
                       };
            var total = list.Count();
            var data = list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return new PageList<User> { total=total, data=data.ToList() };
        }

        public bool UserUpdate(User model)
        {
            return userrepository.Update(model);
        }
    }
}
